package com.yc.springmvc.controller;

import com.yc.springmvc.bean.Person;
import com.yc.springmvc.bean.Result;
import com.yc.springmvc.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("person")
public class PersonController {

    @Autowired
    private PersonService personService;


    @PostMapping
    public Result save(@RequestBody @Valid Person person, Errors errors) {
        if (personService.save(person) == true){
            return new Result(1, "注册成功", person);
        }
        return new Result(0,"字段验证错误",person);
    }

    @GetMapping
    private Result getByName(String name,String pwd){
        Person person = personService.getByName(name);
        if (person != null && pwd != null && pwd.equals(person.getPwd())){
            return new Result(1,"登陆成功",person);
        }
        return new Result(0,"登陆失败",person);
    }
}
